@extends('admin.layout')
@section('content')
@section('title',$title)
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                {{$title}}
                <small>更快的管理您的商品信息
                    @if(session('info'))
                        <span id="info" class="text-red">{{session('info')}}</span>
                    @endif
                </small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="{{url('/admin/index')}}"><i class="fa fa-dashboard"></i> Home</a></li>
                <li><a href="{{url('/admin/goods/add')}}">商品添加</a></li>
                <li class="active">商品列表</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">快速查看商品列表</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                            <table id="example2" class="table table-bordered table-hover">
                            
                               <form action="{{url('/admin/goods/index')}}">
                                   <div class="row">
                                        <div class="col-md-2">
                                           <div class="form-group">
                                                <select name="num" class="form-control">
                                                    <option
                                                    @if(!empty($request['num']) && $request['num'] == 10) selected="selected"
                                                    @endif>10</option>
                                                    <option
                                                    @if(!empty($request['num']) && $request['num'] == 20) selected="selected"
                                                    @endif>20</option>
                                                    <option
                                                    @if(!empty($request['num']) && $request['num'] == 50) selected="selected"
                                                    @endif>50</option>
                                                    <option
                                                    @if(!empty($request['num']) && $request['num'] == 100) selected="selected"
                                                    @endif>100</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-4 col-md-offset-6" >
                                            <div class="input-group input-group-sm">
                                               <input name="keywords"
                                                value="{{$request['keywords']  or  ''}}" type="text" class="form-control">
                                               <span class="input-group-btn">
                                              <button class="btn btn-info btn-flat" type="submit">搜索</button>
                                              </span>
                                            </div><!-- /input-group -->
                                        </div>
                                   </div>

                               </form>
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>类别</th>
                                    <th>名称</th>
                                    <th>价格</th>
                                    <th>库存</th>
                                    <th style="width:50px">销量</th>
                                    <th style="width:50px">状态</th>
                                    <th>描述</th>
                                    <th>图片</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                              
                               
                                @foreach( $data as $row)
                                
                               <tr>
                                    <td>{{$row -> gid}}</td>
                                    <td>{{ $d[$row -> gid] -> name}}</td>
                                    <td class="td_gname" >{{$row -> gname}}</td>
                                    <td>{{$row -> gpirce}}</td>
                                    <td>{{$row -> gnum}}</td>  
                                    <td>{{$row -> gscl}}</td>
                                    <td class="td_status">
                                    @if($row -> gstatus == 0)
                                    上架
                                    @else
                                    下架
                                    @endif
                                    </td>
                                    <td>{{$row -> gdesc}}</td>
                                    <td><img style="width:80px;height:40px" src="{{ asset($row -> gpic)}}"></td>

                                    <td>
                                        <a href="{{ url('/admin/goods/edit') }}/{{ $row -> gid }}">编辑</a>
                                        <a onclick="return confirm('确认要删除吗?');" href="{{ url('/admin/goods/delete') }}/{{ $row -> gid }}">删除</a>
                                        <a href="{{ url('/admin/goods/goodsDoinfo') }}/{{ $row -> gid }}">查看详情</a>
                                </tr>
                                @endforeach
                                </tfoot>
                            </table>
                            {{ $data -> appends($request) -> links() }}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->

    //点击修改信息
    <script>


        window.onload = function()
        {
            //配置ajax选项
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var dbl = true;
            //当点击状态触发事件
            $(".td_status").on('dblclick',function()
            {
                if(!dbl){ return;}
                dbl = false;

                var tda = $(this);
                var status = $(this).html();
                
                
                var gid = $(this).prev().prev().prev().prev().prev().prev().html();
                

                
                $.ajax({

                    url:"{{ url('/admin/goods/changegstatus') }}",
                    type:'GET',
                    data:{gid:gid,status:status},
                    success:function(data)
                    {

                      
                        if(data == 1)
                        {

                            tda.html('下架');
                        }else 
                        {
                            tda.html('上架');
                        }
                    },
                    error:function()
                    {

                    },
                    dataType:'TEXT'
                });
                dbl = true;
            });

            //判断是否已经触发点击事件
            var isDbl = true;

            $(".td_gname").on('dblclick',function(){

                //判断当存在点击事件就return
                if(!isDbl){ return; };
                isDbl = false;

                //把对象赋给td
                var td = $(this);


                //获取 id的值
                // var id = $(this).parent().child().html();
                var gid = $(this).prev().prev().html();
                

               
                //获取原来的值
                var old_value = $(this).html();
                //创建input元素
                var inp = $('<input type="text" />');
                //将原来value的值写入input
                inp.val(old_value);
                //将td改为input
                $(this).html(inp);

                //获取焦点
                inp.select();

                //失去焦点事件
                inp.on('blur',function(){

                    //获取新值
                    var new_value = $(this).val();

                    //ajax发送
                     $.ajax({
                        //还是跳转到本页面
                        url:"{{ url('/admin/goods/changegname') }}",
                        //get方式
                        type:'GET',

                        //发送新修改的键为id的id值name的new_value
                        data:{gid:gid,gname:new_value},


                        //成功执行
                        success:function(data)
                        {
                            if(data == '1')
                            {
                                td.html(new_value);
                            }else
                            {
                                td.html(old_value);
                            }    

                        },

                        //失败执行
                        error:function()
                        {

                        },

                        //返回类型
                        dataType:'TEXT'


                     });

                     isDbl = true;
                    
                });

            });
                
          $('#info').fadeOut(1000);  
        }

    </script>
@endsection